package com.neusoft.liduan.d0922;

//4.使用两个线程打印 1-100. 线程1, 线程2 交替打印
public class Demo4 {
	public static void main(String[] args) {
		Ti it = new Ti();
		Thread t1 = new Thread(it);
		t1.setName("线程1");
		t1.start();
		Thread t2 = new Thread(it);
		t2.setName("线程2");
		t2.start();
	}
	
	private static class Ti implements Runnable {
		int c = 100;
		int i = 0;
		
		@Override
		public void run() {
//			for(int i = 0; i < 100; i++) {
////				System.out.println(Thread.currentThread().getName() + ":::" + i);
//				m(i);
//			}
			while(i < 100) {
				synchronized (this) {
					try {
						System.out.println(Thread.currentThread().getName() + ":::" + i);
						i++;
						notify();
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
		
//		private synchronized void m(int i) {
//			try {
//				notify();
//				wait();
//				System.out.println(Thread.currentThread().getName() + ":::" + i);
//				i++;
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//		}
	}
}
